<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-10-16
 * Time: 17:58
 */

namespace App\Http\Controllers\Mobile;


use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\CustomersVail;
use App\Model\Backstage\Customers;
use App\Tool\Error;
use Illuminate\Http\Request;
// 制定允许其他域名访问
header("Access-Control-Allow-Origin:*");
// 响应类型
header('Access-Control-Allow-Methods:GET, POST');
// 响应头设置
header('Access-Control-Allow-Headers:x-requested-with, content-type');
class CustomerAndRoomController extends Controller
{

    public function dataList(Request $request)
    {
        $parameter = $request->except('apptoken');
        $validator = CustomersVail::condition($parameter);
        if (!$validator || !$request->input('keywords')){
            return $this->error(Error::PARAM_MISSING);
        }
        $authories = explode(',', $request->input('authories'));//该员工拥有的所有权限

        //客源查看权限   全部 108  跨部 109   本部 110 本人 111
        $where = [];//客源的过滤条件
        $where[] = ['c_status', '=', 1];//过滤非回收站客源
        $where[] = ['c_user_phone', '=', $request->input('keywords')];//过滤非回收站客源
        if (in_array(109, $authories)){ //跨部
            $where[] = ['c_store_id', '<>', $parameter['system_id']];
        } elseif (in_array(110, $authories)){//本部
            $where[] = ['c_store_id', '=', $parameter['did']];
        } elseif (in_array(111, $authories)){//本人
            $where[] = ['c_employees_id', '=', $parameter['uid']];
        }

        //房源查看权限   全部 1  跨部 2   本部 3 本人 4
        $map = [];//房源的过滤条件
        $map[] = ['room_status', '=', 1];//过滤非回收站房源
        $map[] = ['owner_phone', '=', $request->input('keywords')];//业主电话
        if (in_array(2, $authories)){ //跨部
            $map[] = ['r_d_id', '<>', $parameter['system_id']];
        } elseif (in_array(3, $authories)){//本部
            $map[] = ['r_d_id', '=', $parameter['did']];
        } elseif (in_array(4, $authories)){//本人
            $map[] = ['source_uid', '=', $parameter['uid']];
        }

        $customers = new Customers();
        $list = $customers->getListForKeywords($where, $map, $parameter['uid'], $authories);
        if (sizeof($list['customersData']) == 0 && sizeof($list['roomData']) == 0) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }
}